Skip to content

New Example #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 11, 2015
Merged

New Example #10

merged 5 commits into from
Feb 11, 2015

Conversation

joneshf
Copy link
Member

@joneshf joneshf commented Jan 25, 2015

Closes #9

Re: @davidchambers

@davidchambers
Copy link

Thanks, @joneshf! I find this much easier to follow than the current example.

@jdegoes
Copy link

jdegoes commented Jan 26, 2015

This looks great, it would also be nice to get an example of decoding the same JSON structure.

@davidchambers
Copy link

Feel free to copy parts of this Stack Overflow answer if it's helpful.

@joneshf
Copy link
Member Author

joneshf commented Feb 2, 2015

Added decode stuff.

@jdegoes
Copy link

jdegoes commented Feb 2, 2015

👍 :shipit:

@kanterov
Copy link

kanterov commented Feb 3, 2015

Don't you get confused by these Unix/Windows line endings changes? Probably we should update gulp to enforce line endings in src/Data/Argonaut/README.md?

@joneshf
Copy link
Member Author

joneshf commented Feb 3, 2015

I just ignore it or if it's too cumbersome use this one weird trick: https://github.com/tiimgreen/github-cheat-sheet#ignore-whitespace

That is: https://github.com/purescript-contrib/purescript-argonaut/pull/10/files?w=1

Also, isn't the line endings a git config thing?

@bburdette
Copy link

Haha just came here to contribute this same thing - an updated example:

module Test where

import Debug.Trace
import Data.Argonaut
import qualified Data.Argonaut.Encode as E
import qualified Data.Argonaut.Decode as D
import Data.Identity
import Data.Either
import Data.Maybe
import qualified Data.StrMap as M
import Data.Tuple

data Foo = Foo
    { foo :: String
    , bar :: Number
    }

instance showFoo :: Show Foo where
  show (Foo f) = "Foo(" ++ show f.foo ++ ", " ++ show f.bar ++ ")"

instance decodeFoo :: D.DecodeJson Foo where
  decodeJson json = maybe (Left "not json for foo") Right $ do
    obj <- toObject json
    foo <- (M.lookup "foo" obj >>= toString)
    bar <- (M.lookup "bar" obj >>= toNumber)
    pure (Foo {foo: foo, bar: bar})

instance encodeFoo :: E.EncodeJson Foo where
  encodeJson (Foo {foo = f, bar = b}) =
    fromObject $   M.fromList [Tuple "foo" $ fromString f,  
                  Tuple "bar" $ fromNumber b] 

main = do 
  let mahfoo = Foo { foo: "meh", bar: 23 } 
  trace $ "show mahfoo: " ++ show mahfoo 
  trace $ "encode mahfoo: " ++ show (encodeJson mahfoo) 

@joneshf
Copy link
Member Author

joneshf commented Feb 11, 2015

Guess I'll merge it then so more people don't do the same.

joneshf added a commit that referenced this pull request Feb 11, 2015
@joneshf joneshf merged commit 5102aa3 into purescript-contrib:master Feb 11, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

Update README
5 participants